//package top.tenebrous.yzl.grpc;
//
//import com.alibaba.fastjson.JSON;
//import com.google.protobuf.Any;
//import io.grpc.stub.StreamObserver;
//import lombok.extern.slf4j.Slf4j;
//import net.devh.boot.grpc.server.service.GrpcService;
//import org.springframework.beans.factory.annotation.Autowired;
//import top.tenebrous.yzl.context.JesticContext;
//import top.tenebrous.yzl.enums.FailCodeEnum;
//import top.tenebrous.yzl.enums.TaskEnum;
//import top.tenebrous.yzl.factorys.JesticPropertyChainFactory;
//import top.tenebrous.yzl.grpc.proto.authority.AuthorityRequest;
//import top.tenebrous.yzl.grpc.proto.authority.AuthorityResponse;
//import top.tenebrous.yzl.grpc.proto.authority.AuthorityServerGrpc;
//import top.tenebrous.yzl.grpc.proto.common.GrpcResponse;
//import top.tenebrous.yzl.grpc.proto.common.RetCode;
//import top.tenebrous.yzl.model.request.JesticAuthorityRequest;
//import top.tenebrous.yzl.model.response.JesticAuthorityResponse;
//import top.tenebrous.yzl.service.IAuthorityService;
//import top.tenebrous.yzl.utils.JesticContextInstance;
//
//import javax.annotation.Resource;
//import java.util.Objects;
//
///**
// * @author yazhang6
// */
//@Slf4j
//@GrpcService
//public class AuthorityServiceGrpcImpl extends AuthorityServerGrpc.AuthorityServerImplBase {
//
//    @Autowired
//    IAuthorityService authorityService;
//
//    @Resource
//    private JesticPropertyChainFactory<JesticAuthorityRequest, JesticAuthorityResponse> jesticPropertyChainFactory;
//
//    @Override
//    public void doAuthority(AuthorityRequest request, StreamObserver<GrpcResponse> responseObserver) {
//        // 构建上下文对象
//        JesticAuthorityRequest authorityRequest = buildRequest(request);
//        log.info("jestic [AuthorityServiceGrpcImpl.authority] server [{}] successfully established connection， request \n[{}]", authorityRequest.getServerName(), JSON.toJSONString(authorityRequest, true));
//
//        JesticContext<JesticAuthorityRequest, JesticAuthorityResponse> context = JesticContextInstance.initContext(authorityRequest, new JesticAuthorityResponse(),TaskEnum.AUTHORITY);
//        jesticPropertyChainFactory.getHandler(context.getTaskEnum().getCode()).handler(context);
//        JesticAuthorityResponse contextResponse = context.getResponse();
//
//        if (Objects.equals(contextResponse.getCode(), FailCodeEnum.SERVER_NOT_FOUND.getCode())) {
//            GrpcResponse response = GrpcResponse.newBuilder()
//                    .setRetMsg(RetCode.BIZ_ERROR.name())
//                    .setErrorCode(String.valueOf(contextResponse.getCode()))
//                    .setErrorMsg(FailCodeEnum.SERVER_NOT_FOUND.getZhDesc())
//                    .build();
//            responseObserver.onNext(response);
//        } else if (Objects.equals(contextResponse.getCode(), FailCodeEnum.SERVER_NOT_RUNNING.getCode())) {
//            GrpcResponse response = GrpcResponse.newBuilder()
//                    .setRetMsg(RetCode.BIZ_ERROR.name())
//                    .setErrorCode(String.valueOf(contextResponse.getCode()))
//                    .setErrorMsg(FailCodeEnum.SERVER_NOT_RUNNING.getZhDesc())
//                    .build();
//            responseObserver.onNext(response);
//        } else {
//            AuthorityResponse authorityResponse = AuthorityResponse.newBuilder()
//                    .setId(contextResponse.getId())
//                    .setExpire(contextResponse.getExpire())
//                    .setInstanceStatus(contextResponse.getInstanceStatus())
//                    .setIsAuth(contextResponse.getIsAuth())
//                    .setInstanceId(contextResponse.getInstanceId())
//                    .setServerName(contextResponse.getServerName())
//                    .setToken(contextResponse.getToken())
//                    .setHealth(contextResponse.getHealth())
//                    .setServerId(contextResponse.getServerId())
//                    .build();
//            GrpcResponse response = GrpcResponse.newBuilder().setRetMsg(RetCode.SUCCESS.name()).setData(Any.pack(authorityResponse)).build();
//            responseObserver.onNext(response);
//        }
//        responseObserver.onCompleted();
//    }
//
//    private JesticAuthorityRequest buildRequest(AuthorityRequest request) {
//        JesticAuthorityRequest authorityRequest = new JesticAuthorityRequest();
//        authorityRequest.setServerId(request.getServerId());
//        authorityRequest.setServerName(request.getServerName());
//        authorityRequest.setIp(request.getIp());
//        authorityRequest.setPort(String.valueOf(request.getPort()));
//        authorityRequest.setGrpcPort(String.valueOf(request.getGrpcPort()));
//        return authorityRequest;
//    }
//
//}
